﻿using System.Windows;

namespace Super.Hmi.Utils
{
    public class LanguageHelper
    {
        public static readonly Lazy<LanguageHelper> _instance = new Lazy<LanguageHelper>(() => new LanguageHelper());
        public static LanguageHelper Instance => _instance.Value;
        private LanguageHelper() { }
        /// <summary>
        /// 切换语言
        /// </summary>
        /// <param name="language"></param>
        public void SwitchLanguage(string language)
        {
            if (language == null) return;
            language = language == "Chinese" ? "zh-CN" : language == "Japanese" ? "ja-JP" : "en-US";
            string resourcePath = $"/Assets/Languages/Language.{language}.xaml";
            var dict = new ResourceDictionary { Source = new Uri(resourcePath, UriKind.Relative) };
            // 移除旧语言资源
            var oldDict = Application.Current.Resources.MergedDictionaries.FirstOrDefault(d => d.Source?.OriginalString?.Contains("Language.") == true);
            if (oldDict != null)
            {
                Application.Current.Resources.MergedDictionaries.Remove(oldDict);
            }
            // 添加新语言资源
            Application.Current.Resources.MergedDictionaries.Add(dict);
            //ThemeSettingHelper.Instance.SaveTheme();
        }
    }
}
